package com.example.peanutsubway.dao.impl;

import com.example.peanutsubway.dao.ChannelDao;
import com.example.peanutsubway.entity.Channel;
import com.example.peanutsubway.entity.PageInfo;
import com.example.peanutsubway.util.JDBCUtils;
import com.example.peanutsubway.vo.ChannelVo;


import java.util.List;

/**
 * @Author：liyangsong
 * @Version：1.0
 * @Date：2021/10/15-10:55
 * @Since:jdk1.8
 * @Description:
 */


public class ChannelDaoImpl implements ChannelDao {
    @Override
    public List<ChannelVo> showAllChannel(Channel channel, PageInfo pageInfo) {
        StringBuilder sql = new StringBuilder("select pn.*,p.cname fname ,pc.cname sname from pn_channel pn,pn_channel_sort p,pn_channel_sort pc WHERE pn.cid = p.cid and pn.sid = pc.cid and pn.area is not NULL");
        if (channel != null) {
            if (channel.getChannelNum() != null && !"".equals(channel.getChannelNum())) {
                sql.append(" and channelNum = '%" + channel.getChannelNum() + "%' ");
            }
        }
        sql.append(" limit ?,?");
        return JDBCUtils.find(sql.toString(), ChannelVo.class, pageInfo.getStartIndex(), pageInfo.getPageNum());
    }

    @Override
    public Integer Channelcount(Channel channel) {
        StringBuilder sql = new StringBuilder("select count(1) from pn_channel pn,pn_channel_sort p,pn_channel_sort pc WHERE pn.cid = p.cid and pn.sid = pc.cid and pn.area is not NULL");
        if (channel != null) {
            if (channel.getChannelNum() != null && !"".equals(channel.getChannelNum())) {
                sql.append(" and channelNum = '%" + channel.getChannelNum() + "%' ");
            }
        }
        return JDBCUtils.count(sql.toString()).intValue();
    }

    @Override
    public Boolean addChannel(Channel channel) {
        String sql = "INSERT into pn_channel VALUES(NULL,?,?,?,\"https://www.baidu.com/\",?,?,NOW(),?,?,?)";
        channel.setChannelNum(channel.getCid() + channel.getSid() + channel.getProductName());
        return JDBCUtils.insert(sql,
                channel.getCid(),
                channel.getSid(),
                channel.getChannelNum(),
                channel.getProductName(),
                channel.getShowName(),
                channel.getArea(),
                channel.getPlatform(),
                channel.getDes()) > 0;
    }

    @Override
    public Boolean addAdress(String downloadAdress, Integer id) {
        String sql = " update pn_channel set downloadAdress = CONCAT(downloadAdress,\";\",?)  WHERE id = ?";
        return JDBCUtils.update(sql, downloadAdress, id) > 0;
    }

    @Override
    public Boolean deteleAdress(Integer id) {
        String sql = "UPDATE pn_channel set area = NULL WHERE id = ?";
        return JDBCUtils.update(sql, id) > 0;
    }
}
